package cn.isjinhao.se.structure.impl;

import cn.isjinhao.se.structure.KeySupported;

import java.util.Objects;

/**
 * 保证name和clazz都不为null
 *
 * @Author ISJINHAO
 * @Date 2021/7/2 14:04
 */
public class ColumnMetaInfo implements KeySupported<ColumnMetaInfo> {

    private String name;
    private Class<?> clazz;

    public ColumnMetaInfo(String name, Class<?> clazz) {
        if (clazz == null || name == null) {
            throw new IllegalArgumentException("无法使用 null 初始化列的元信息类型！");
        }
        this.clazz = clazz;
        this.name = name;
    }

    public boolean checkTypeMatch(Object object) {
        if(object == null) {
            return true;
        }
        if(Cell.class.equals(object.getClass())) {
            object = ((Cell) object).getObject();
        }
        return object == null || clazz.isAssignableFrom(object.getClass());
    }

    public String getName() {
        return name;
    }

    public Class<?> getClazz() {
        return clazz;
    }

    @Override
    public String toString() {
        return "name: " + this.name + ", clazz: " + this.clazz;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ColumnMetaInfo that = (ColumnMetaInfo) o;
        return Objects.equals(name, that.name) && Objects.equals(clazz, that.clazz);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, clazz);
    }

    @Override
    public ColumnMetaInfo key() {
        return this;
    }
}
